require('page/common/header/index.js');

var _util = require('util/util.js');
var _catalog_service = require('service/catalog-service.js');
var listTemplate = require('./index.string');


var catalogList = {
    listDate : {
        title       : '',
        headList    : [],
        productList : {},
        itemList    : {}
    },
    productHeadList : [
        'Product ID',
        'Name'
    ],
    itemHeadList    : [
        'Item ID',
        'Product ID',
        'Description',
        'List Price',
        ''
    ],
    init: function () {
        this.bindEvents();
        this.loadAccountInfo();
        return this;
    },
    bindEvents : function () {
    },
    loadAccountInfo : function(){
        var id = _util.getURLParam('id');
        var route = _util.getURLParam('route');
        var _this = this;
        // console.log("id"+id);
        // console.log("route"+route);
        if(route === 'product'){
            _catalog_service.getProductList(
                id,
                function(res){
                    console.log(res);
                    _this.listDate.headList = _this.productHeadList;
                    _this.listDate.title = id;
                    _this.listDate.productList = res;
                    _this.listDate.itemList = null;

                    var result = _util.renderHtml(listTemplate, {ListData : _this.listDate});
                    //console.log("list"+result);
                    $('#Catalog').html(result);
                },
                function(){
                }
            );
        }
        if(route === 'item'){
            _catalog_service.getItemList(
                id,
                function(res){
                    //console.log(res);
                    _this.listDate.headList = _this.itemHeadList;
                    _this.listDate.title = id;
                    _this.listDate.productList = null;
                    _this.listDate.itemList = res;

                    var result = _util.renderHtml(listTemplate, {ListData : _this.listDate});
                    //console.log(result);
                    $('#Catalog').html(result);
                },
                function(){
                }
            );
        }
    }
}
module.exports = catalogList.init();